<?php
declare (strict_types = 1);

namespace app\model\communal;

use app\facade\StringFacade;
use app\service\logic\common\chat\ChatService;
use think\db\Query;
use think\Model;
use app\model\concern\AmisTrait;
use think\model\concern\SoftDelete;
use think\model\relation\MorphTo;

/**
 * Class app\model\communal\ImMessageModel
 */
class ImMessageModel extends Model
{
    use AmisTrait;

    /**
     * 是否显示在右边 1 右边 2 左边
     */
    const IS_SHOW_RIGHT_ONE = 1;
    const IS_SHOW_RIGHT_TWO = 2;

    /**
     * 状态 1 未读 2 已读 3 撤回
     */
    const STATE_ONE = 1;
    const STATE_TWO = 2;
    const STATE_THREE = 3;

    /**
     * 消息类型 1 文本 2 图片 3 咨询订单发起通话 4 私聊专家信息
     */
    const TYPE_ONE = 1;
    const TYPE_TWO = 2;
    const TYPE_THREE = 3;
    const TYPE_FOUR = 4;

    public static $typeMap = [
        self::TYPE_ONE => '文本',
        self::TYPE_TWO => '图片',
        self::TYPE_THREE => '咨询订单发起通话',
        self::TYPE_FOUR => '私聊专家信息',
    ];

    /**
     * 多态类型 1 客服 2 用户
     */
    const MORPH_TYPE_ONE = 1;
    const MORPH_TYPE_TWO = 2;

    /**
     * 多态字段
     */
    public static $morph = [
        self::MORPH_TYPE_ONE => AdminModel::class,
        self::MORPH_TYPE_TWO => UserModel::class,
    ];

    public static $morphMap = [
        AdminModel::class => self::MORPH_TYPE_ONE,
        UserModel::class => self::MORPH_TYPE_TWO,
    ];

    /**
     * 数据库配置
     *
     * @var string
     */
    protected $connection = 'communal';

    /**
     * 主键
     *
     * @var string
     */
    protected $pk = 'id';

    /**
     * 表名
     *
     * @var string
     */
    protected $table = '56000_im_message';

    /**
     * 自动时间戳
     *
     * @var bool
     */
    protected $autoWriteTimestamp = true;

    /**
     * 时间字段设置
     *
     * @var string
     */
    protected $createTime = 'created_at';
    protected $updateTime = 'updated_at';

    // /**
    //  * 软删除
    //  */
    // use SoftDelete;

    /**
     * 展示字段名
     *
     * @var string[]
     */
    public static $column = [
        'id', 'content', 'sender_id', 'sender_type', 'receiver_id', 'receiver_type', 'type', 'state', 'created_at'
    ];

    /**
     * 搜索字段名
     *
     * @var string[]
     */
    public static $searchColumn = [
        'id', 'sender_id', 'sender_type', 'receiver_id', 'receiver_type', 'user_id', 'user_type',
        'createtime', 'type', 'state',

        /*
         * 后台搜索
         */
        'admin_id', 'customer_id', 'admin_ids',
    ];

    /**
     * 新增字段
     *
     * @var string[]
     */
    public static $saveColumn = ['content', 'sender_id', 'sender_type', 'receiver_id', 'receiver_type', 'type'];

    /**
     * 更新字段
     *
     * @var string[]
     */
    public static $updateColumn = ['state'];

    /**
     * 参与人搜索
     *
     * @param Query $query
     * @param $value
     * @param $data
     */
    public function searchUserIdAttr($query, $value, $data)
    {
        if (!is_null($value) && strlen((string)$value) > 0) {
            $chatMark = invoke(ChatService::class)->getChatMark($data, 'user_type', 'login_type', 'user_id', 'login_id', false);
            $query->where('chat_mark', $chatMark);
        }
    }

    /**
     * 参与人类型
     *
     * @param $query
     * @param $value
     * @return bool
     */
    public function searchUserTypeAttr($query, $value)
    {
        return true;
    }

    /**
     * 发送人搜索
     *
     * @param $query
     * @param $value
     */
    public function searchSenderIdAttr($query, $value)
    {
        if (!empty($value)) {
            $query->where('sender_id', $value);
        }
    }

    /**
     * 发送类型搜索
     *
     * @param $query
     * @param $value
     */
    public function searchSenderTypeAttr($query, $value)
    {
        if (!empty($value)) {
            $type = self::$morph[$value];
            $query->where('sender_type', $type);
        }
    }

    /**
     * 接收人搜索
     *
     * @param $query
     * @param $value
     */
    public function searchReceiverIdAttr($query, $value)
    {
        if (!empty($value)) {
            $query->where('receiver_id', $value);
        }
    }

    /**
     * 接收类型搜索
     *
     * @param $query
     * @param $value
     */
    public function searchReceiverTypeAttr($query, $value)
    {
        if (!empty($value)) {
            $type = self::$morph[$value];
            $query->where('receiver_type', $type);
        }
    }

    /**
     * 员工ID 搜索
     *
     * @param Query $query
     * @param $value
     */
    public function searchAdminIdAttr($query, $value)
    {
        if (!empty($value)) {
            $type = self::$morph[1];
            $query->where(function ($q) use ($value, $type) {
                $q->whereOr([
                    [['sender_id', '=', $value], ['sender_type', '=', $type]],
                    [['receiver_id', '=', $value], ['receiver_type', '=', $type]]
                ]);
            });
        }
    }

    /**
     * 员工IDS 搜索
     *
     * @param Query $query
     * @param $value
     */
    public function searchAdminIdsAttr($query, $value)
    {
        if (!empty($value)) {
            $type = self::$morph[1];
            $query->where(function ($q) use ($value, $type) {
                $q->whereOr([
                    [['sender_id', 'in', $value], ['sender_type', '=', $type]],
                    [['receiver_id', 'in', $value], ['receiver_type', '=', $type]]
                ]);
            });
        }
    }

    /**
     * 客户/用户ID 搜索
     *
     * @param Query $query
     * @param $value
     */
    public function searchCustomerIdAttr($query, $value)
    {
        if (!empty($value)) {
            $type = self::$morph[2];
            $query->where(function ($q) use ($value, $type) {
                $q->whereOr([
                    [['sender_id', '=', $value], ['sender_type', '=', $type]],
                    [['receiver_id', '=', $value], ['receiver_type', '=', $type]]
                ]);
            });
        }
    }

    /**
     * 修改
     *
     * @param $value
     * @param $data
     * @return mixed
     */
    public function setContentAttr($value, $data)
    {
        if (2 == $data['type']) {
            $value = StringFacade::replaceDomainShowUrl($value);
        }
        return $value;
    }

    /**
     * 获取
     *
     * @param $value
     * @param $data
     * @return mixed
     */
    public function getContentAttr($value, $data)
    {
        if (2 == $data['type']) {
            $value = StringFacade::splicingDomainShowUrl($value);
        }
        return $value;
    }

    /**
     * 多态关联发送人
     *
     * @return MorphTo
     */
    public function sender()
    {
        return $this->morphTo();
    }

    /**
     * 多态关联接收人
     *
     * @return MorphTo
     */
    public function receiver()
    {
        return $this->morphTo();
    }
}
